package com.example.sql_2_code_demo.service.impl;

import com.example.sql_2_code_demo.entity.OrderReceiveAddress;
import com.example.sql_2_code_demo.dao.OrderReceiveAddressDao;
import com.example.sql_2_code_demo.service.OrderReceiveAddressService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 收货地址表(OrderReceiveAddress)表服务实现类
 *
 * @author makejava
 * @since 2022-10-09 10:01:38
 */
@Service("orderReceiveAddressService")
public class OrderReceiveAddressServiceImpl implements OrderReceiveAddressService {
    @Resource
    private OrderReceiveAddressDao orderReceiveAddressDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public OrderReceiveAddress queryById(Long id) {
        return this.orderReceiveAddressDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param orderReceiveAddress 筛选条件
     * @param pageRequest         分页对象
     * @return 查询结果
     */
    @Override
    public Page<OrderReceiveAddress> queryByPage(OrderReceiveAddress orderReceiveAddress, PageRequest pageRequest) {
        long total = this.orderReceiveAddressDao.count(orderReceiveAddress);
        return new PageImpl<>(this.orderReceiveAddressDao.queryAllByLimit(orderReceiveAddress, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param orderReceiveAddress 实例对象
     * @return 实例对象
     */
    @Override
    public OrderReceiveAddress insert(OrderReceiveAddress orderReceiveAddress) {
        this.orderReceiveAddressDao.insert(orderReceiveAddress);
        return orderReceiveAddress;
    }

    /**
     * 修改数据
     *
     * @param orderReceiveAddress 实例对象
     * @return 实例对象
     */
    @Override
    public OrderReceiveAddress update(OrderReceiveAddress orderReceiveAddress) {
        this.orderReceiveAddressDao.update(orderReceiveAddress);
        return this.queryById(orderReceiveAddress.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.orderReceiveAddressDao.deleteById(id) > 0;
    }
}
